package utils

import (
	"fmt"
	"github.com/yusufpapurcu/wmi"
	"net"
	"strings"
)

//网卡
type Win32_NetworkAdapter struct {
	Description string `json:"description,omitempty"`
	MACAddress  string `json:"mac_address,omitempty"`
}

func NetworkAdapter() []Win32_NetworkAdapter {
	var dst []Win32_NetworkAdapter
	q := wmi.CreateQuery(&dst, "where PhysicalAdapter = 'TRUE'")
	err := wmi.Query(q, &dst)
	if err != nil {
		fmt.Println("出现了错误。。。。")
	}
	var result []Win32_NetworkAdapter
	for _, v := range dst {
		if strings.Contains(v.Description, "TAP") == false && strings.Contains(v.Description, "VPN") == false && strings.Contains(v.Description, "Bluetooth") == false && strings.Contains(v.Description, "Virtual") == false {
			result = append(result, v)
		}
	}
	return result
}

//处理器
type Win32_Processor struct {
	Name string `json:"name,omitempty"`
}

func Processor() []Win32_Processor {
	var dst []Win32_Processor
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		fmt.Println("出现了错误。。。。")
	}
	var result []Win32_Processor
	for _, item := range dst {
		result = append(result, item)
	}
	return result
}

//内存
type Win32_PhysicalMemory struct {
	Manufacturer string `json:"manufacturer,omitempty"`
	PartNumber   string `json:"part_number,omitempty"`
	BankLabel    string `json:"bank_label,omitempty"`
	Capacity     int    `json:"capacity,omitempty"`
}

func Memory() []Win32_PhysicalMemory {
	var dst []Win32_PhysicalMemory
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		fmt.Println("出现了错误。。。。")
	}
	var result []Win32_PhysicalMemory
	for _, item := range dst {
		result = append(result, item)
	}
	return result
}

//计算机型号
type Win32_ComputerSystemProduct struct {
	Vendor            string `json:"vendor,omitempty"`
	Version           string `json:"version,omitempty"`
	Name              string `json:"name,omitempty"`
	IdentifyingNumber string `json:"identifying_number,omitempty"`
}

func Computer() Win32_ComputerSystemProduct {
	var dst []Win32_ComputerSystemProduct
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		fmt.Println("出现了错误。。。。")
	}
	var result Win32_ComputerSystemProduct
	for _, item := range dst {
		result = item
	}
	return result
}

//计算机名
type Win32_ComputerSystem struct {
	DNSHostName string `json:"dns_host_name,omitempty"`
	Domain      string `json:"domain,omitempty"`
}

func Host() Win32_ComputerSystem {
	var dst []Win32_ComputerSystem
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		fmt.Println("出现了错误。。。。")
	}
	var result Win32_ComputerSystem
	for _, item := range dst {
		result = item
	}
	return result
}

//检测服务器是否可以访问

func IsConnected() bool {
	_, err := net.Dial("tcp", "10.10.1.1:123")
	if err != nil {
		return false
	}
	return true
}
